import axios from "axios"
import router from "../router"
import { Toast } from "vant"

// 请求拦截：每一次发请求，都要执行的逻辑，可以在此处书写
/* 
请求拦截的回调函数：
参数：请求配置项
返回值：是后端收到的真正请求信息
*/
axios.interceptors.request.use(config => {
    //  如果访问的不是登录和注册接口，那么就要再请求头携带token
    if (config.url != '/api/register' && config.url != '/api/login') {
        config.headers.authorization = JSON.parse(localStorage.getItem("isLogin")).token;
    }
    // console.group("发请求了")
    // console.log(config)
    // console.groupEnd()
    return config
})

// 响应拦截
axios.interceptors.response.use(res => {
    // 统一打印数据
    // console.group("本次请求已完成，请求的地址是:" + res.config.url)
    // console.log(res)
    // console.groupEnd()

    //    统一处理失败
    if (res.data.code != 200) {
        console.log(res);
        Toast(res.data.msg)
    }

    // 统一处理掉线
    if (res.data.msg === "登录已过期或访问权限受限") {
        // 跳转到登录
        router.replace("/login")
    }

    return res
})

// 注册接口
export const reqregister = (user) => {
    return axios({
        url: "/api/register",
        method: "post",
        data: user,
    })
}

// 登录接口
export const reqlogin = (user) => {
    return axios({
        url: "/api/login",
        method: "post",
        data: user,
    })
}

// 首页分类
export const reqhomecate = () => {
    return axios({
        url: "/api/getcate",
        method: "get",
    })
}

// 首页轮播
export const reqhomebanner = () => {
    return axios({
        url: "/api/getbanner",
        method: "get",
    })
}

// 商品首页
export const reqhomegoods = () => {
    return axios({
        url: "/api/gethortgoods",
        method: "get",
    })
}

// 分类
export const reqcate = () => {
    return axios({
        url: "/api/getcates",
        method: "get"
    })
}

// 搜索接口
export const reqsearch = (user) => {
    return axios({
        url: "/api/search",
        method: "get",
        params: user
    })
}

// 商品列表
export const reqlist = (user) => {
    return axios({
        url: "/api/getgoodlist",
        method: "get",
        params: user
    })
}

// 商品详情
export const reqdetail = (user) => {
    return axios({
        url: "/api/getgoodsinfo",
        method: "get",
        params: user
    })
}

// 购物车添加
export const reqcartadd = (user) => {
    return axios({
        url: "/api/cartadd",
        method: "post",
        data: user,
        // headers: {
        //     authorization: JSON.parse(localStorage.getItem("isLogin")).token
        // },
    })
}

// 购物车列表
export const reqcartlist = (user) => {
    return axios({
        url: "/api/cartlist",
        method: "get",
        params: user,
        // headers: {
        //     authorization: JSON.parse(localStorage.getItem("isLogin")).token
        // },
    })
}

// 购物车修改
export const reqcartedit = (user) => {
    return axios({
        url: "/api/cartedit",
        method: "post",
        data: user,
        // headers: {
        //     authorization: JSON.parse(localStorage.getItem("isLogin")).token
        // },
    })
}

// 购物车删除
export const reqcartdel = (user) => {
    return axios({
        url: "/api/cartdelete",
        method: "post",
        data: user,
        // headers: {
        //     authorization: JSON.parse(localStorage.getItem("isLogin")).token
        // },
    })
}

// 地址添加
export const reqaddressadd = p => {
    return axios({
        url: "/api/addressadd",
        method: "post",
        data: p,

    })
}

// 地址删除
export const reqaddressremove = p => {
    return axios({
        url: "/api/addressremove",
        method: "post",
        data: p,

    })
}

// 地址编辑
export const reqaddressedit = p => {
    return axios({
        url: "/api/addressedit",
        method: "post",
        data: p,

    })
}


// 地址列表
export const reqaddresslist = p => {
    return axios({
        url: "/api/addresslist",
        method: "get",
        params: p,
    })
}


// 订单生成
export const reqorderadd = p => {
    return axios({
        url: "/api/orderadd",
        method: "post",
        data: p,
    })
}
